package me.zhengjie.sqlservice.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import me.zhengjie.mapper.WcsOutboundMissionMapper;
import me.zhengjie.pojo.entity.WcsOutboundMission;
import me.zhengjie.sqlservice.WcsOutboundMissionService;
import me.zhengjie.utility.state.TaskStatus;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.Collections;
import java.util.List;

@Service
@RequiredArgsConstructor
public class WcsOutboundMissionServiceImpl extends ServiceImpl<WcsOutboundMissionMapper, WcsOutboundMission> implements WcsOutboundMissionService {
    private final WcsOutboundMissionMapper wcsOutboundMissionMapper;

    @Override
    public boolean insertOutbound(WcsOutboundMission wcsOutboundMission) {
        return wcsOutboundMissionMapper.insert(wcsOutboundMission) > 0;
    }

    @Override
    public WcsOutboundMission selectOutboundCode(String robotTaskCode) {
        LambdaQueryWrapper<WcsOutboundMission> wcsOutboundMissionLambdaQueryWrapper = new LambdaQueryWrapper<>();
        wcsOutboundMissionLambdaQueryWrapper.eq(WcsOutboundMission::getOutboundCode,robotTaskCode);
        return wcsOutboundMissionMapper.selectOne(wcsOutboundMissionLambdaQueryWrapper);
    }

    @Override
    public boolean updateMissionStatusAndAgvId(Integer id, Integer missionStatus, String agvId) {
        LambdaUpdateWrapper<WcsOutboundMission> wcsOutboundMissionLambdaUpdateWrapper = new LambdaUpdateWrapper<>();
        wcsOutboundMissionLambdaUpdateWrapper.set(WcsOutboundMission::getMissionStatus,missionStatus)
                .set(WcsOutboundMission::getAgvId,agvId)
                .set(WcsOutboundMission::getUpdateTime, LocalDateTime.now())
                .eq(WcsOutboundMission::getId,id);
        return wcsOutboundMissionMapper.update(null,wcsOutboundMissionLambdaUpdateWrapper) >0;
    }

    @Override
    public boolean updateMissionStatus(String outboundCode, Integer missionStatus) {
        LambdaUpdateWrapper<WcsOutboundMission> wcsOutboundMissionLambdaUpdateWrapper = new LambdaUpdateWrapper<>();
        wcsOutboundMissionLambdaUpdateWrapper.set(WcsOutboundMission::getMissionStatus,missionStatus)
                .set(WcsOutboundMission::getUpdateTime,LocalDateTime.now())
                .eq(WcsOutboundMission::getOutboundCode,outboundCode);
        return wcsOutboundMissionMapper.update(null,wcsOutboundMissionLambdaUpdateWrapper) >0;
    }

    @Override
    public boolean deleteOutboundId(Integer id) {
        return wcsOutboundMissionMapper.deleteById(id) > 0;
    }

    @Override
    public List<WcsOutboundMission> selectStatusAndDestinationAndContainerTypePage(Integer missionStatus, String destination, Integer containerType, Integer size) {
        Page<WcsOutboundMission> wcsInboundMissionPage = new Page<>(1, size);
        LambdaQueryWrapper<WcsOutboundMission> wcsInboundMissionLambdaQueryWrapper = new LambdaQueryWrapper<>();
        wcsInboundMissionLambdaQueryWrapper.eq(WcsOutboundMission::getMissionStatus,missionStatus)
                .eq(WcsOutboundMission::getDestination,destination)
                .eq(WcsOutboundMission::getContainerType,containerType);
        return wcsOutboundMissionMapper.selectPage(wcsInboundMissionPage,wcsInboundMissionLambdaQueryWrapper).getRecords();
    }

    @Override
    public List<WcsOutboundMission> selectTask(Integer missionStatus, Integer containerType) {
        LambdaQueryWrapper<WcsOutboundMission> wcsOutboundMissionLambdaQueryWrapper = new LambdaQueryWrapper<>();
        wcsOutboundMissionLambdaQueryWrapper.eq(WcsOutboundMission::getMissionStatus,missionStatus)
                .eq(WcsOutboundMission::getContainerType,containerType);
        return wcsOutboundMissionMapper.selectList(wcsOutboundMissionLambdaQueryWrapper);
    }

    @Override
    public List<WcsOutboundMission> selectMissionStatus(Integer status) {
        LambdaQueryWrapper<WcsOutboundMission> wcsOutboundMissionLambdaQueryWrapper = new LambdaQueryWrapper<>();
        wcsOutboundMissionLambdaQueryWrapper.eq(WcsOutboundMission::getMissionStatus,status);
        return wcsOutboundMissionMapper.selectList(wcsOutboundMissionLambdaQueryWrapper);
    }

    @Override
    public List<WcsOutboundMission> selectLtStatus(Integer status) {
        LambdaQueryWrapper<WcsOutboundMission> wcsOutboundMissionLambdaQueryWrapper = new LambdaQueryWrapper<>();
        wcsOutboundMissionLambdaQueryWrapper.lt(WcsOutboundMission::getMissionStatus,status);
        return wcsOutboundMissionMapper.selectList(wcsOutboundMissionLambdaQueryWrapper);
    }

    @Override
    public List<WcsOutboundMission> selectContainerCodeIn(List<String> containerCodeList) {
        LambdaQueryWrapper<WcsOutboundMission> wcsOutboundMissionLambdaQueryWrapper = new LambdaQueryWrapper<>();
        wcsOutboundMissionLambdaQueryWrapper.in(WcsOutboundMission::getContainerCode,containerCodeList)
                .in(WcsOutboundMission::getMissionStatus,4,5);
        return wcsOutboundMissionMapper.selectList(wcsOutboundMissionLambdaQueryWrapper);
    }

    @Override
    public List<WcsOutboundMission> selectStatus(Integer status) {
        LambdaQueryWrapper<WcsOutboundMission> wcsOutboundMissionLambdaQueryWrapper = new LambdaQueryWrapper<>();
        wcsOutboundMissionLambdaQueryWrapper.eq(WcsOutboundMission::getMissionStatus,status);
        return wcsOutboundMissionMapper.selectList(wcsOutboundMissionLambdaQueryWrapper);
    }

    @Override
    public boolean updateMissionStatusFluentFrameStorage(String outboundCode, Integer status, String storageCode) {
        LambdaUpdateWrapper<WcsOutboundMission> wcsOutboundMissionLambdaUpdateWrapper = new LambdaUpdateWrapper<>();
        wcsOutboundMissionLambdaUpdateWrapper.set(WcsOutboundMission::getMissionStatus,status)
                .set(WcsOutboundMission::getUpdateTime,LocalDateTime.now())
                .set(WcsOutboundMission::getFluentFrameStorage,storageCode)
                .eq(WcsOutboundMission::getOutboundCode,outboundCode);
        return wcsOutboundMissionMapper.update(null,wcsOutboundMissionLambdaUpdateWrapper) >0;

    }

    @Override
    public boolean updateDestination(Integer id, String destination) {
        LambdaUpdateWrapper<WcsOutboundMission> wcsOutboundMissionLambdaUpdateWrapper = new LambdaUpdateWrapper<>();
        wcsOutboundMissionLambdaUpdateWrapper.set(WcsOutboundMission::getDestination,destination)
                .set(WcsOutboundMission::getUpdateTime,LocalDateTime.now())
                .eq(WcsOutboundMission::getId,id);
        return wcsOutboundMissionMapper.update(null,wcsOutboundMissionLambdaUpdateWrapper)>0;
    }

    @Override
    public List<WcsOutboundMission> selectContainerCodeNotTask(String containerCode) {
        LambdaQueryWrapper<WcsOutboundMission> wcsOutboundMissionLambdaQueryWrapper = new LambdaQueryWrapper<>();
        wcsOutboundMissionLambdaQueryWrapper.eq(WcsOutboundMission::getContainerCode,containerCode)
                .ne(WcsOutboundMission::getMissionStatus, TaskStatus.Task_complete.getStatus())
                .ne(WcsOutboundMission::getMissionStatus, TaskStatus.Task_cancel.getStatus());
        return wcsOutboundMissionMapper.selectList(wcsOutboundMissionLambdaQueryWrapper);
    }

    @Override
    public List<WcsOutboundMission> selectStatusAndDestinationAndContainerType(Integer missionStatus, String destination, Integer containerType) {
        LambdaQueryWrapper<WcsOutboundMission> wcsInboundMissionLambdaQueryWrapper = new LambdaQueryWrapper<>();
        wcsInboundMissionLambdaQueryWrapper.eq(WcsOutboundMission::getMissionStatus,missionStatus)
                .eq(WcsOutboundMission::getDestination,destination)
                .eq(WcsOutboundMission::getContainerType,containerType)
                .orderByAsc(WcsOutboundMission::getCreateTime);
        return wcsOutboundMissionMapper.selectList(wcsInboundMissionLambdaQueryWrapper);
    }
}
